package com.kaaction.modules.eb.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kaaction.common.utils.CacheUtils;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.common.web.BaseController;
import com.kaaction.modules.eb.entity.Coupon;
import com.kaaction.modules.eb.entity.CouponDetail;
import com.kaaction.modules.eb.service.CouponDetailService;
import com.kaaction.modules.eb.service.CouponService;
import com.kaaction.modules.httppost.Sendsms;

@Controller
@RequestMapping(value = "/api/v1/eb/coupon")
public class CouponApiController extends BaseController{
	
	private final String COUPON_SMS_CODE = "coupon_sms_code";
	
	@Autowired
	private CouponService couponService;
	
	@Autowired
	private CouponDetailService couponDetailService;
	
	/**
	 * 获取优惠券前，验证用户身份
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = { "sms" })
	public void sms(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String phone = request.getParameter("phone");
		String smsCode = StringUtils.getCodeWithSixNum();
		String content = "a:"+smsCode + "。";
		logger.info("smsCode is : " + smsCode);
		request.getSession().setAttribute(COUPON_SMS_CODE, smsCode);
		CacheUtils.put(phone, smsCode);
		Map map = new HashMap();
		map.put("phone", phone);
		map.put("content",  "感谢您选择喀嗒，" + smsCode  + "为您的验证码(15分钟有效)，请尽快完成验证，谢谢！");
		map.put("type", "01");
		super.writeAjaxResponse(response, Sendsms.captcha(map));
		
	}
	
	/**
	 * 获取优惠券列表
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = { "list" })
	public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Coupon> list = couponService.findList(new Coupon());
		super.writeAjaxResponse(response, list);
		
	}
	
	/**
	 * 领取验证码
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = { "get" })
	public void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String phone = request.getParameter("phone");
		String smsCode = request.getParameter("smsCode");
		String couponId = request.getParameter("couponId");
		
		String smsCodeInSession = String.valueOf(CacheUtils.get(phone));
		
		CouponDetail couponDetail = new CouponDetail();
		if(!StringUtils.isBlank(smsCode) && smsCode.equals(smsCodeInSession)) {
			//TODO 放到业务层
			Coupon coupon = couponService.get(couponId);
			couponDetail.setCoupon(coupon);
			List<CouponDetail> list = couponDetailService.findList(couponDetail);
			if(list != null && list.size() > 0){
				couponDetail = list.get(0);
				if(couponDetail != null){
					couponDetail.setStatus("1");
					couponDetail.setRemarks(phone);
					couponDetailService.save(couponDetail);
					int remainCouponNum = coupon.getRemainNum();
					coupon.setRemainNum(remainCouponNum - 1);
					couponService.save(coupon);
				}
			}
		}
		super.writeAjaxResponse(response, couponDetail);
		
	}
}
